//
//  SpellLibrary.m
//  Duoduo
//
//  Created by abc on 14-2-27.
//  Copyright (c) 2014年 zuoye. All rights reserved.
//

#import "SpellLibrary.h"
#import "NSString+Additions.h"

@implementation SpellLibrary

+ (SpellLibrary*)instance {
    static SpellLibrary* g_spellLibrary;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        g_spellLibrary = [[SpellLibrary alloc] init];
    });
    return g_spellLibrary;
}

- (NSMutableString *)getSpellForWord:(NSString*)word {
    NSMutableString *spell = [NSMutableString stringWithString:word];
    CFRange range = CFRangeMake(0, spell.length);
    CFStringTransform((CFMutableStringRef)spell, &range, kCFStringTransformMandarinLatin, NO);
    
    //对 ǖǘǚǜ 进行特殊处理：
    NSString *tempString = spell;

    tempString = [tempString stringByReplacingOccurrencesOfString:@"ǖ" withString:@"v" options:NSLiteralSearch range:NSMakeRange(0, tempString.length)];
    tempString = [tempString stringByReplacingOccurrencesOfString:@"ǘ" withString:@"v" options:NSLiteralSearch range:NSMakeRange(0, tempString.length)];
    tempString = [tempString stringByReplacingOccurrencesOfString:@"ǚ" withString:@"v" options:NSLiteralSearch range:NSMakeRange(0, tempString.length)];
    tempString = [tempString stringByReplacingOccurrencesOfString:@"ǜ" withString:@"v" options:NSLiteralSearch range:NSMakeRange(0, tempString.length)];

    spell = [NSMutableString stringWithString:tempString];
    
    CFStringTransform((CFMutableStringRef)spell, &range, kCFStringTransformStripCombiningMarks, NO);
    //spell = (NSMutableString*)[spell removeAllSpace];//取消空格
    return spell;
}

- (NSString*)briefSpellWordFromSpellArray:(NSArray*)sender fullWord:(int)count {
    NSMutableString* briefSpell = [[NSMutableString alloc] init];
    for (int index = 0; index < [sender count]; index ++) {
        NSString* fullSpell = sender[index];
        if ([fullSpell length] == 0) {
            continue;
        }
        if (index < count) {
            [briefSpell appendString:fullSpell];
        } else {
            NSString* briefSpellAtIndex = [fullSpell substringToIndex:1];
            [briefSpell appendString:briefSpellAtIndex];
        }
    }
    return briefSpell;
}

@end
